package cn.doctor.assistant.pad.ui.ziliao;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.doctor.assistant.pad.R;
import cn.doctor.assistant.pad.api.huifang.ReCallApi;
import cn.doctor.assistant.pad.api.ziliao.InformatApi;
import cn.doctor.assistant.pad.base.BaseNetActivity;
import cn.doctor.assistant.pad.base.ResultCallback;
import cn.doctor.assistant.pad.entity.Course;
import cn.doctor.assistant.pad.entity.FollowedBean;
import cn.doctor.assistant.pad.entity.Record;
import cn.doctor.assistant.pad.ui.UI;
import cn.doctor.assistant.pad.ui.huifang.AddRecallActivity;

/**
 * Created by jiaolf on 2016/4/1.
 * 住院记录详情
 */
public class ZhuYuanDetailActivity extends BaseNetActivity {

    public static final int REQUEST_CODE_BINGCHENG = 100; // 添加病程
    public static final int REQUEST_CODE_RECALL = 101; // 添加回访

    Record mRecord;
    @InjectView(R.id.titlebar_left_title)
    TextView titlebarLeftTitle;
    @InjectView(R.id.titlebar_right_text)
    TextView titlebarRightText;
    @InjectView(R.id.titlebar_text_add_bingcheng)
    TextView titlebarTextAddBingcheng;
    @InjectView(R.id.icon)
    ImageView icon;
    @InjectView(R.id.text_name)
    TextView textName;
    @InjectView(R.id.text_keshi)
    TextView textKeshi;
    @InjectView(R.id.text_doctor)
    TextView textDoctor;
    @InjectView(R.id.text_zhuyuanhao)
    TextView textZhuyuanhao;
    @InjectView(R.id.text_diagnose)
    TextView textDiagnose;
    @InjectView(R.id.text_start_date)
    TextView textStartDate;
    @InjectView(R.id.text_end_date)
    TextView textEndDate;
    @InjectView(R.id.text_patient_from)
    TextView textPatientFrom;
    @InjectView(R.id.listview_bingcheng)
    ListView listviewBingcheng;
    @InjectView(R.id.listview_huifang)
    ListView listviewHuifang;

    MyAdapter mBingChengAdapter;
    MyAdapter mHuifangAdapter;

    List<Course> bingChengData = new ArrayList<>();
    List<FollowedBean> huifangData = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuyuan_detail);
        ButterKnife.inject(this);

        setTitle("住院记录详情");
        titlebarRightText.setText("新增回访记录");
        titlebarTextAddBingcheng.setText("新增病程记录");

        mRecord = (Record) getIntent().getExtras().getSerializable("record");
        initialize();
    }

    void initialize() {
        textName.setText(mRecord.getName());

        textKeshi.setText("入住科室："+ (mRecord.getDept() == null ? "" : mRecord.getDept()));
        textDoctor.setText("主治医生："+ (mRecord.getMajor() == null ? "" : mRecord.getMajor()));
        textZhuyuanhao.setText("住院号："+ (mRecord.getInhiscode() == null ? "" : mRecord.getInhiscode()));
        textDiagnose.setText("入院诊断："+ (mRecord.getNote() == null ? "" : mRecord.getNote()));

        String ruyaunData = mRecord.getAdmissionDate();
        textStartDate.setText("入院日期：" + (ruyaunData == null ? "" : ruyaunData.substring(0,10)));

        String chuyuanData = mRecord.getDischargeDate();
        textEndDate.setText("出院日期：" + (chuyuanData == null ? "" : chuyuanData.substring(0,10)));

        textPatientFrom.setText("病人来源：" + mRecord.getOrigin());

        listviewBingcheng.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UI.startActivity(BingChengDetailActivity.class,
                        "course", bingChengData.get(position),
                        "record", mRecord);
            }
        });

        getBingChengInfo();
        getRecallInfo();
    }

    void getBingChengInfo(){
        // get data, set adapter...
        showProgress("加载中...");
        InformatApi.getBingCheng(mRecord.getId(), new ResultCallback<List<Course>>() {
            @Override
            public void onSuccess(List<Course> result) {
                stopProgress();
                if (result != null && result.size() > 0) {
                    bingChengData.clear();
                    bingChengData.addAll(result);

                    List<MyAdapter.ItemModel> listModel = new ArrayList<>();
                    mBingChengAdapter = new MyAdapter(listModel);
                    for (Course c : bingChengData) {
                        MyAdapter.ItemModel model = mBingChengAdapter.new ItemModel();
                        model.note = c.getContent();
                        model.date = c.getDate();
                        listModel.add(model);
                    }
                    listviewBingcheng.setAdapter(mBingChengAdapter);
                }
            }
        });
    }

    void getRecallInfo() {
        // get data, set adapter...
        showProgress("加载中...");
        ReCallApi.getFollowedByRecord(mRecord.getId(), new ResultCallback<List<FollowedBean>>() {
            @Override
            public void onSuccess(List<FollowedBean> result) {
                stopProgress();
                if (result != null && result.size() > 0) {
                    huifangData.clear();
                    huifangData.addAll(result);

                    List<MyAdapter.ItemModel> listModel = new ArrayList<>();
                    mHuifangAdapter = new MyAdapter(listModel);
                    for (FollowedBean f : huifangData) {
                        MyAdapter.ItemModel model = mHuifangAdapter.new ItemModel();
                        model.note = f.getContent();
                        model.date = f.getDate();
                        listModel.add(model);
                    }
                    listviewHuifang.setAdapter(mHuifangAdapter);
                }
            }
        });
    }


    @OnClick({R.id.titlebar_left_title, R.id.titlebar_right_text, R.id.titlebar_text_add_bingcheng})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.titlebar_left_title:
                back();
                break;
            case R.id.titlebar_right_text:
                //UI.startActivity(AddRecallActivity.class, "record", mRecord);
                Intent intentRecall = new Intent(this, AddRecallActivity.class);
                intentRecall.putExtra("record", mRecord);
                startActivityForResult(intentRecall, REQUEST_CODE_RECALL);
                break;
            case R.id.titlebar_text_add_bingcheng:
                //UI.startActivity(AddBingChengActivity.class, "record", mRecord);
                Intent intnetBC = new Intent(this, AddBingChengActivity.class);
                intnetBC.putExtra("record", mRecord);
                startActivityForResult(intnetBC, REQUEST_CODE_BINGCHENG);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_BINGCHENG) {
                Course course = (Course) data.getExtras().getSerializable("course");
                bingChengData.add(course);

                if (mBingChengAdapter == null) {
                    List<MyAdapter.ItemModel> items = new ArrayList<>();
                    mBingChengAdapter = new MyAdapter(items);
                    listviewBingcheng.setAdapter(mBingChengAdapter);
                }

                mBingChengAdapter.add(course);

            }else if(requestCode == REQUEST_CODE_RECALL) {
                FollowedBean fb = (FollowedBean) data.getExtras().getSerializable("followed");
                huifangData.add(fb);

                if (mHuifangAdapter == null) {
                    List<MyAdapter.ItemModel> items = new ArrayList<>();
                    mHuifangAdapter = new MyAdapter(items);
                    listviewHuifang.setAdapter(mHuifangAdapter);
                }

                mHuifangAdapter.add(fb);
            }
        }
    }

    class MyAdapter extends BaseAdapter {

        List<ItemModel> items;

        public MyAdapter(List<ItemModel> items) {
            this.items = items;
        }

        public void add(FollowedBean fb) {
            ItemModel im = new ItemModel();
            im.note = fb.getContent();
            im.date = fb.getDate();
            items.add(im);
            notifyDataSetChanged();
        }

        public void add(Course course) {
            ItemModel im = new ItemModel();
            im.note = course.getContent();
            im.date = course.getDate();
            items.add(im);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return items.size();
        }

        @Override
        public Object getItem(int position) {
            return items.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(ZhuYuanDetailActivity.this).inflate(R.layout.list_item_with_three_text, parent, false);
                viewHolder = new ViewHolder(convertView);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            ItemModel model = items.get(position);
            viewHolder.listItemId.setText(String.valueOf(position + 1));
            viewHolder.listItemNote.setText(model.note);
            viewHolder.listItemDate.setText(model.date);

            return convertView;
        }

        class ViewHolder {
            @InjectView(R.id.list_item_id)
            TextView listItemId;
            @InjectView(R.id.list_item_note)
            TextView listItemNote;
            @InjectView(R.id.list_item_date)
            TextView listItemDate;

            ViewHolder(View view) {
                ButterKnife.inject(this, view);
            }
        }

        public class ItemModel {
            String note; // 内容
            String date; // 日期
        }
    }
}
